package com.dubbo.gmail.provider.remoteCache;

import com.dubbo.gmail.api.model.UserModel;
import com.dubbo.gmail.api.remotecache.AbstractRemoteCache;
import com.dubbo.gmail.api.service.ReceiveHelloService;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.ReferenceBean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
public class RemoteCacheProvider extends AbstractRemoteCache implements ApplicationContextAware {

    public ApplicationContext applicationContext;
    private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    @DubboReference(check = false)
    ReceiveHelloService receiveHelloService;

    private Runnable sendMessageRunnable = () -> {
        getMap().values().forEach(e -> {
            URL url = (URL) e;
//            sendDelayMessage(url, applicationContext);
            try {
                sendDelayMessage();
            } catch (Exception exception) {
                exception.printStackTrace();
            } finally {
            }
        });
    };

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        scheduledExecutorService.scheduleAtFixedRate(sendMessageRunnable, 1, 1, TimeUnit.SECONDS);
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void sendDelayMessage(URL url, ApplicationContext context) {
//        String url = "dubbo://localhost:20880/com.dubbo.gmail.api.service.HelloService";//更改不同的Dubbo服务暴露的ip地址&端口
        StringBuilder stringBuilder = new StringBuilder();
        String urlString = stringBuilder.append(url.getProtocol()).append("://").append(url.getHost()).append(":")
                .append("20881").append("/").append("com.dubbo.gmail.api.service.ReceiveHelloService").toString();
        System.out.println(urlString);
        ReferenceBean<ReceiveHelloService> referenceBean = new ReferenceBean<>();
        referenceBean.setApplicationContext(context);
        referenceBean.setInterface(com.dubbo.gmail.api.service.ReceiveHelloService.class);
        referenceBean.setUrl(urlString);
        try {
            referenceBean.afterPropertiesSet();
            ReceiveHelloService helloService = referenceBean.get();
            helloService.receiveHelloService(new UserModel(123, "ddddsfa"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendDelayMessage() {
        receiveHelloService.receiveHelloService(new UserModel(123, "ddddsfa"));
    }
}
